package javaTest;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class Library_file extends DriverClass {
	
	//write a program to check palindrome.. 
	
	public static void palindrome() {
		// TODO Auto-generated method stub
		int num=131;
		int a=num;
		
		int r=0;
		int p=0;
	while(a!=0){
	 r=a%10;
	p=p*10+r;
	a=a/10;		
	}
		if(num==p){
			System.out.println("No is Palindrome");

	}
		System.out.println("No is not Palindrome");

}
//2. Write a program to find odd or even 
	public static void evenOROdd() {
		
		int a=5;
		
		if(a%2==0)
		{
			
			System.out.println("The no is even");
		}
		else
		{
			System.out.println("No is odd");
		}
	}
	//3. write a program to find largest of three numbers.
	public static void FindLargest() {

		int a=100;
		int b=20;
		int c=30;
		
		if(a>b && a>c)
		{
			System.out.println("A is greater among three " +a);
		}
		else if(b>a & b >c)
		{
			System.out.println("B is greater amont three " +b);
		}
		else if(c>a && c>b)
		{
			System.out.println("C is greater "+c);
		}

	}
	//4. writ a program to print prime numbers. 
	public static void PrimeNumber() {
		// TODO Auto-generated method stub
        int limit = 20;
        
   
        for(int i=1; i < limit; i++){
               
          boolean isPrime = true;
               
                //check to see if the number is prime
                for(int j=2; j < i ; j++){
                       
                        if(i % j == 0){
                   isPrime = false;
                                break;
                        }
                }
                // print the number
                if(isPrime)
                        System.out.print(i + " ");
        }
}
//5. write a program to find factorial	
	public static void FindFactorial(){
		// TODO Auto-generated method stub

		int a,b,c;
		
		a=6;
		c=1;
		
		if(a<0)
		{
			System.out.println("Please eneter non -ive no ");
		}
		else
		{
			for(b=1;b<=a;b++)
			
				c=c*b;
				System.out.println("The Factorial of "+a+"is "+c);
			
		}
	}

//6. write a program to bubble sort. And 6 i/p has to take from user as bubble elements.
	public  static void Bubblesort() {
		 int  n,c, d, swap;
		  Scanner in = new Scanner(System.in);
		 
		   System.out.println("Input number of integers to sort");
		  n = in.nextInt();
		 
		    int array[] = new int[n];
		 
		    System.out.println("Enter " + n + " integers");
		 
		    for (c = 0; c < n; c++) 
		      array[c] = in.nextInt();
		 
		    for (c = 0; c < ( n - 1 ); c++) {
		      for (d = 0; d < n - c - 1; d++) {
		        if (array[d] > array[d+1]) /* For descending order use < */
		        {
		          swap       = array[d];
		          array[d]   = array[d+1];
		          array[d+1] = swap;
		        }
		      }
		    }
		 
		    System.out.println("Sorted list of numbers");
		 
		    for (c = 0; c < n; c++) 
		      System.out.println(array[c]);
		    
		    //  in.close();
}
//7. write a program for linear search. And 5 i/p has to take from user as linear elements. 
	public static void linearSearch() {
		int c,n,search,array[];
	    
	    Scanner in = new Scanner(System.in);
	    System.out.println("Enter number of elements");
	    n = in.nextInt(); 
	    array = new int[n];
	 
	    System.out.println("Enter " + n + " integers");
	 
	    for (c = 0; c < n; c++)
	      array[c] = in.nextInt();
	 
	    System.out.println("Enter value to find");
	    search = in.nextInt();
	 
	    for (c = 0; c < n; c++)
	    {
	      if (array[c] == search)     /* Searching element is present */
	      {
	         System.out.println(search + " is present at location " + (c + 1) + ".");
	          break;
	      }
	   }
	   if (c == n)  /* Searching element is absent */
	      System.out.println(search + " is not present in array.");
	   
	   in.close();
	  }
//8. write a program for binary search. And 5 i/p has to take from user as binary elements. 
	public static void BinarySearch() {
		 int c, first, last, middle, n, search, array[];
		 
		    Scanner in = new Scanner(System.in);
		    System.out.println("Enter number of elements");
		    n = in.nextInt(); 
		    array = new int[n];
		 
		    System.out.println("Enter " + n + " integers");
		 
		 
		    for (c = 0; c < n; c++)
		      array[c] = in.nextInt();
		 
		    System.out.println("Enter value to find");
		    search = in.nextInt();
		 
		    first  = 0;
		    last   = n - 1;
		    middle = (first + last)/2;
		 
		    while( first <= last )
		    {
		      if ( array[middle] < search )
		        first = middle + 1;    
		      else if ( array[middle] == search ) 
		      {
		        System.out.println(search + " found at location " + (middle + 1) + ".");
		        break;
		      }
		      else
		         last = middle - 1;
		 
		      middle = (first + last)/2;
		   }
		   if ( first > last )
		      System.out.println(search + " is not present in the list.\n");
		   
	      in.close();

		  }
	
//9. write a program to generate 10 random numbers. 

	public  static void RandomNo() {
		// TODO Auto-generated method stub
		
		int a=10;
		int b;
		
		b= 1+(int)(Math.random()*a);
		
		System.out.println(b);
		
		

	}
//10. Write a program TO reverse the digits of the NUMBERS in java programming? 
				
			public static void reverse(int number)
			{
				
				int b=0;
				int c=0;
				
				do
				{
					b=number%10;
					c=c*10+b;
					number=number/10;
				}
				while(number>0);
				
			System.out.println("You entered "+number+"and its reverse no is :"+c);
							
					
				
			}


	

//11. Consider there is a 3 boolean variable called a, b, c. Check if at least two out of three booleans are true

	public  static void Check_boolean() {
		// TODO Auto-generated method stub
boolean a=true;
boolean b= false;
boolean c=true;

if(a==b && b==true)
{
	System.out.println("A and B are true");
}
else{
	System.out.println("A & B are false");
}
if (a==c && c==true)
{
	System.out.println("A and C are true");
}
else{
	System.out.println("A & C are false");
}
if (b==c && c== true)
{
	System.out.println("B and C are true");
}
else{
	System.out.println("B & C are false");
}
	}

//12. Write a programme to find area of triangle, Rectangle and square. 
public static void triangle(double a,double b)
{
	double area=(a*b)/2;
	
	System.out.println("The area of triangle is "+area);
//return area;
}

public static void Rectangle(int width,int height)
{
int area=width*height;
System.out.println("The area of triangle is "+area);
//return area;
}
public static void square(int a)
{
int area=a*a;
//return area;
System.out.println("The area of triangle is "+area);
}


//13.Write a java program to check a number is Armstrong number? 
public static void armstrong() {
	 int n, sum = 0, temp, r;
	 
     Scanner in = new Scanner(System.in);
     System.out.println("Enter a number to check if it is an armstrong number");      
     n = in.nextInt();

     temp = n;

     while( temp != 0 )
     {
        r = temp%10;
        sum = sum + r*r*r;
        temp = temp/10; 
     }

     if ( n == sum )
        System.out.println("Entered number is an armstrong number.");
     else
        System.out.println("Entered number is not an armstrong number.");
     
  in.close();
  }
//14. Write a java program to sorting the elements in the array 
public static void ArraySort() {
	// TODO Auto-generated method stub

	int[] arr=new int[10];
	for(int j=0;j<10;j++){
		int p=10;
		int k=1+(int)(Math.random()*p);
		arr[j]=k;
	}
	for(int k=0;k<arr.length;k++)
	{
	//	System.out.println("Before Sorting :"+arr[k]);
		Arrays.sort(arr);
		System.out.println("After sorting "+arr[k]);
		
		
	}
}

//*15. Write a program in Java to find the Least Common Multiple (L.C.M.) of two numbers entered by the user. 



		
		public static void FindLCM(){
			System.out.print("Enter the 1st number : ");
			Scanner sc= new Scanner(System.in);
			int a=sc.nextInt();
			System.out.print("Enter the 2nd number : ");
			int b=sc.nextInt();
		int num1=a;
		int num2=b;
		
		if(a>b){
		 num1= a;
		 num2 =b;
		}
		else{
		num1=b;
		num2=a;
		
		}
	for (int i = 1; i <= num2; i++) {
        if((num1*i)%num2==0){
        	int k= i*num1;
        	System.out.println("The LCM is: "+k);
        	
        }
        	}
	throw new Error ("Error");
        	}
        	
//16. Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N. 
		public static void consecutiveNumber() throws IOException {
			// TODO Auto-generated method stub
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			System.out.print("Enter a number : "); //inputting the number
			int a=Integer.parseInt(br.readLine());
			int sum=0,j=0;
			for(int i=1;i<a;i++)
			{
				sum=i;
				j=i+1;
				/* adding consecutive natural numbers till sum is less than the given number*/
				while(sum<a){
					sum=sum+j;
					j++;
				}
				
				/* when the above while condition is false, then either the sum is equal to
				that number or is greater than that number
				So, we will be printing the series of consecutive numbers only if the sum is
				equal to that number
		
				Note: variable 'i' is keeping record of the first number of the series and
				variable 'j' is keeping record of the last number of the series */
				if(sum==a){
					for(int k=i;k<j;k++){
						if(k==i)
							System.out.print(k);
						else
							System.out.print(" + "+k);
						}
					System.out.println();
					}
				}
			}
//17. Write a program to find the shortest and the longest word in a sentence and print them along with their length. 
		public static void shortestLongest ()throws IOException
	    {
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	        System.out.print("Enter any sentence : ");
	        String s=br.readLine();
	        int len=s.length(); //finding the length of the sentence
	        String x="",maxw="",minw="";
	        int p,maxl=0,minl=len;
	        StringTokenizer str = new StringTokenizer(s,";?'. :,"); //converting String into StringTokenizer object
	        int c=str.countTokens();
	        for(int i=0;i<c;i++)
	        {
	            x=str.nextToken(); //extracting tokens (i.e. words) one at a time
	            p=x.length();
	                if(p<minl) //checking for minimum length
	                {
	                    minl=p;
	                    minw=x;
	                }
	                if(p>maxl) //checking for maximum length
	                {
	                    maxl=p;
	                    maxw=x;
	                }
	        }
	        System.out.println("Shortest word = "+minw+"\nLength = "+minl);
	        System.out.println("Longest word = "+maxw+"\nLength = "+maxl);
	    }
		
//18. Write a program to input a number. Count and print the frequency of each digit present in that number.  
		public static void digitsFreq() throws NumberFormatException, IOException {
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int n;
			System.out.println("Enter number");
			n=Integer.parseInt(br.readLine());
			int A[]=new int[9];
			int a=0;
			int c=0;
			for(int i=n;i!=0;i/=10)
			{
				int d=i%10;
				A[a]=d;
				a++;
			}
			for(int i=0;i<=9;i++)
			{
				for(int j=0;j<i;j++)
				{
					if(A[j]==i)
					{
						c++;

					}
				}
				if(c!=0)
					System.out.println("The digit "+i+" occurs "+c+" times"); 
				c=0; 
			}
		} 
		
//19. Write a program in JAVA to find the Prime factors of a number. [Sample i/p = 24 O/p =  2, 2, 2, 3]
		public static  void Find_PrimeNo() {
			// TODO Auto-generated method stub
	        int limit = 20;
	        
	   
	        for(int i=1; i < limit; i++){
	               
	          boolean isPrime = true;
	               
	                //check to see if the number is prime
	                for(int j=2; j < i ; j++){
	                       
	                        if(i % j == 0){
	                   isPrime = false;
	                                break;
	                        }
	                }
	                // print the number
	                if(isPrime)
	                        System.out.print(i + " ");
	        }
	
		}
//20. Write a program to to print fibonacci series.
		public static void FibonicSeries() {
			
			int a,b,c;
			c=5;
			
			
			a=b=1;
			
			for(int n=1;n<=c;n++)
			{
				int sum;
				sum=a+b;
				a=b;
				b=sum;
				System.out.println(sum);
				
				
			}}
			
//21.Write a java program to perform ascending order sort for any random 10 numbers		
		public static void ascendingSort() {
			{
				System.out.print("Please enter number for sorting: ");
				Scanner input = new Scanner(System.in);
				int[] num_in_array = new int[6];
				
				for(int enter_num=0; enter_num<6; enter_num++){
					num_in_array[enter_num] = input.nextInt();

					for(int sort_num=0; sort_num<6; sort_num++){
						if(num_in_array[enter_num] < num_in_array[sort_num]){
							int swap = num_in_array[enter_num];
			                    num_in_array[enter_num] = num_in_array[sort_num];
			                    num_in_array[sort_num] = swap;
			                }
			            }
			        }
			        System.out.println("Sorted Numbers are: ");
			 
			        for(int print = 0; print<6; print++)
			        {
			            System.out.print(num_in_array[print]+ " ");
			        }
			        System.out.println();
					input.close();
			}
		}	

//21.  Shape
		public static  void Shape() {
			
			int a=10;
			
			for(int i=1;i<a;i++)
			{
				for(int j=1;j<i;j++)
				{
					System.out.print("*");
				}
				System.out.println("");
			}
			

			}

//	Swap two variable without using 3rd variable.	
		public static  void SwapTwoNoWithoutUsingThird() {
			// TODO Auto-generated method stub

			int a=10;
			int b=20;
			a=a+b;
			b=a-b;
			a=b-a;
			System.out.println("A:- "+a+" B:-"+b);
		}
		
		
//24. Write java program to get the out put as below  (Get * as input)		

		
		public static void Shapes() {
			for(int i=1; i<= 5 ;i++){
				for(int j=0; j < i; j++){
					System.out.print("*");
					}
		            System.out.println("");
				}
			for (int x=0 ; x<4 ; x++){
				for(int y=4; y>x ; y--){
					System.out.print("*");
				}
				System.out.println("");
			}
		}
//25.write a java program to  Create a new folder in Java and name it as Framework 
		public static void createDir(){
			String dirname="C:/Users/amandeep/Google Drive/Framework";
			boolean sucess=(new File (dirname)).mkdir();
			//System.out.println(sucess+"- is true or fals");
			if(sucess==true){
				System.out.println("The dir has been made at loc :-"+dirname);
			}else{
				System.out.println("The dir has not been made at loc :-"+dirname);
			}}
//26. write a java program to create a Microsoft xl 
		public static void createXLS() throws IOException{
		
		    String destinationFilePath = "C:/Users/amandeep/Google Drive/Framework/1.xls";

	
		    FileOutputStream fout = new FileOutputStream(destinationFilePath);
		     
		   
		    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		    HSSFWorkbook workBook = new HSSFWorkbook();
		 
		  
		    HSSFSheet spreadSheet = workBook.createSheet("My_Sheet");
		     
		   
		    HSSFRow row = spreadSheet.createRow((short) 0);
		     
		
		    HSSFCell cell;
		     
		  
		    cell = row.createCell(0);
		    cell.setCellValue(new HSSFRichTextString("Aman"));

		    cell = row.createCell(1);
		    cell.setCellValue(new HSSFRichTextString("deep"));
		     
		    workBook.write(outputStream);
		     
		    outputStream.writeTo(fout);
		    outputStream.close();
		    fout.close();
		}
		
//27. Write a java program to delete a folder 
		public static void DeleteDir(String dirname){
		  boolean sucess=(new File(dirname).delete());
		  if(sucess==true){
			  System.out.println("The foleder has been delted");
		  }
		  else {
			  System.out.println("Folder not deleted");
		  }
		  
	  }
//28.Question 28 -- Read Data from Excel File
		public static String[][] readData(String dataTablePath, String sheetName) throws IOException{
			String xlData[][] = null;
			/* Get the Path of the data Table*/
			File xlFile = new File(dataTablePath);

			/* Access the file*/
			FileInputStream xlDoc = new FileInputStream(xlFile);

			/*Access the work Book*/
			HSSFWorkbook wb = new HSSFWorkbook(xlDoc);

			/*Access the sheet*/
			HSSFSheet sheet = wb.getSheet(sheetName);

			int iRowCount = sheet.getLastRowNum()+1;
			int iColCount = sheet.getRow(0).getLastCellNum();

			/* Define length of string array*/
			xlData = new String[iRowCount][iColCount];

			for(int i = 0; i < iRowCount; i++){
				for(int j = 1; j < iColCount; j++){
					HSSFCell cell = sheet.getRow(i).getCell(j);
				//	while (cell.getStringCellValue()!= null)
						xlData[i][j] = cell.getStringCellValue();
					//					System.out.print(cell.getStringCellValue() + " ");
				}
				//				System.out.println();

			}
			return xlData;
		}
//************ String Assignment **********************

//1. Consider the following string:

//String hannah = "Did Hannah see bees? Hannah did.";
//Question: What is the value displayed by the expression hannah.length()? = 32
//Question: What is the value returned by the method call hannah.charAt(12)? =e
//Question: Write an expression that refers to the letter b in the string referred to by hannah.

//2.How long is the string returned by the following expression? What is the string?

//"Was it a car or a cat I saw?".substring(9, 12)  
//Ans : CAR

//3. In the following program, called ComputeResult, what is the value of result after each numbered line executes?		
	//	1   result.setCharAt(0, original.charAt(0));
	//	ANSWER: result = "si"
	//	2   result.setCharAt(1, original.charAt(original.length()-1));
	//	ANSWER: result = "se"
	//	3   result.insert(1, original.charAt(4));
	//	ANSWER: result = "swe"
	//	4   result.append(original.substring(1,4));
	//	ANSWER: result = "sweoft"
	//	5   result.insert(3, (original.substring(index, index+2) + " ")); 
	//	ANSWER: result = "swear oft"

//  Q4 -- Show two ways to concatenate the following two strings together to get the string "Hi, mom.":
	//	String hi = "Hi, ";
	//	String mom = "mom.";
//ANSWER: System.out.print(hi+mom);	
//			OR
//		String sent = hi+mom;
//		System.out.print(sent);
//
//	5. Write a program that computes your initials from your full name and displays them.
		
//7. Swap two numbers using third variable
		public  static void Swap_TwoNoUsingThirdVariable() {
			// TODO Auto-generated method stub

			int a=10;
			int b=20;
			int c=a;
	 a=b;
			b=c;
			System.out.println("A:- "+a+" B:-"+b);
				
		}
			
//8. Swap two number with out using third variable
		public static  void SwapTwoNoWithoutUsingThrd() {
			// TODO Auto-generated method stub

			int a=10;
			int b=20;
			a=a+b;
			b=a-b;
			a=b-a;
			System.out.println("A:- "+a+" B:-"+b);
		}


//9. What is String in Java? String is a data type?
		// String is a primitive data type which gets its support from e java.lang.String class and as soon as we enclose a chracter in double 
		//quotes it creates a new string object.
		
//10. What are different ways to create String Object?
		//String str = "Aman";
 		//  StringBuilder strB = new StringBuilder("Aman");

//11. Write a method to check if input String is Palindrome?
        
		public static void Palindrome() {
			// TODO Auto-generated method stub
			int num=131;
			int a=num;
			
			int r=0;
			int p=0;
		while(a!=0){
		 r=a%10;
		p=p*10+r;
		a=a/10;		
		}
			if(num==p){
				System.out.println("No is Palindrome");

		}
			System.out.println("No is not Palindrome");

}
//12. Write a method that will remove given character from the String?
		public  static void Removecharacter() {
			// TODO Auto-generated method stub
			
			String str= "This is a good place to go";
			
			String st=str.replace("good", "");
			System.out.println("The new string is :"+st);

		}
//13. How can we make String upper case or lower case?
		public static void MakeStringUpperCaseLowerCase(){
			String Upcase="AMAN";
			String Lowcase="aman";
			
			String ch= Upcase.toLowerCase();
			Upcase=ch;
			ch=Lowcase.toUpperCase();
					Lowcase=ch;
		    System.out.println(Upcase);
		    System.out.println(Lowcase);
			
			

		}
		
//14. What is String subSequence method?	
//Ans:This is a  method that returns a new character sequence that is a subsequence of this sequence.
//15. How to compare two Strings in java program?
		public static void CompareToStrings() {
			// TODO Auto-generated method stub
			String c= "I AM HERE";
			String k="i am here";
			String d="I AM TEHRE";
			boolean e= c.equals(d);
			
			boolean f=c.equalsIgnoreCase(k);
			
			if(e==true)
			{
				System.out.println(c+" and "+d+" are equal ");
			}
			else 
			{
				System.out.println(c+" and "+d+" are not equal ");
			}
			if (f==true)
			{
				System.out.println(k+" and "+c+" are equal");
			}
			else 
			{
				System.out.println("Both are not equal:"+k);
			}
			}

//16. How to convert String to char and vice versa?
		public static  void StringToCharANDChartoString(){
			String str="change";
			char ch='A';
			StringToChar(str);
			chartoString(ch);
			
		}
		public static void StringToChar(String str){
			
		
			char[] ch=str.toCharArray();
			
			System.out.println(ch);
		}
		
		public static void chartoString(char ch){
			
			String conv=Character.toString(ch);
			System.out.println("The Char converted to String :"+conv);
		}

//17. How to convert String to byte array and vice versa?
		//Ans:- We can convert String to byte using inbuilt function getbytes(byte[] a = string.getBytes) and similarly to convert byte to string 
	//	String str = new String(a);
		
//18. Can we use String in switch case
		//Yes we can use string in Switch case if we are using Java 7 or later
		
//19. Write a program to print all permutations of String?
//20. Write a function to find out longest palindrome in a given string?
//21. Difference between String, StringBuffer and StringBuilder?
//Ans. The values stored in the String can't be changed where as the values stored in the  StringBuffer and StringBuilder.
//22. Why String is immutable or final in Java.
//Ans.It is mainly for security reasons. String is used as parameter in network connection, database url etc. It can be easily attacked if it is mutable.
//23. How to Split String in java?- 
		
			public static void SplitSpring(){
				// TODO Auto-generated method stub
		String sen= "This is very good";
		System.out.println(java.util.Arrays.toString(sen.split(" ")));
			
		}
//24.Why Char array is preferred over String for storing password?	
//Ans.Strings are immutable. That means once you've created the string, if another process can dump memory, there's no way (aside from reflection) you can get rid of the data before GC kicks in.
//With an array, you can explicitly wipe the data after you're done with it: you can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.
			//25. How do you check if two Strings are equal in Java?	
			public  static void CompareTwoStrings(){
				// TODO Auto-generated method stub
				String c= "I AM HERE";
				String k="i am here";
				String d="I AM TEHRE";
				boolean e= c.equals(d);
				
				boolean f=c.equalsIgnoreCase(k);
				
				if(e==true)
				{
					System.out.println(c+" and "+d+" are equal ");
				}
				else 
				{
					System.out.println(c+" and "+d+" are not equal ");
				}
				if (f==true)
				{
					System.out.println(k+" and "+c+" are equal");
				}
				else 
				{
					System.out.println("Both are not equal:"+k);
				}
				}
					
			
}
